<?php
/*
 *      map.class.php
 *      
 *      Copyright 2009 Valery Tverdohleb <tvsdesign@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

class Map
{
	protected $mapData = array();
	protected $cellDoors = null;
	
	public function __construct($cX, $cY, $cZ, $cL)
	{
		if (isset($cX) && isset($cY) && isset($cZ) && isset($cL))
		{
			$this->load($cX, $cY, $cZ, $cL);
		}
	}
	
	public function isCellActive($dX, $dY, $dZ)
	{
		return $this->mapData[$dX][$dY][$dZ]['active'];
	}
	
	public function doors($coords)
	{
		$this->cellDoors = array();
		//if (null == $this->cellDoors)
		{
			$cX = $coords['X'];
			$cY = $coords['Y'];
			$cZ = $coords['Z'];
			$cL = $coords['L'];
			
			$cellDoors = array();
			$stmt = Engine::get()->db->prepare('SELECT `id`, `X`, `Y`, `Z`, `L`, `loc_id`, `title_i`, `title_o` FROM `location_doors` WHERE (`X` = :X AND `Y` = :Y  AND `Z` = :Z AND `L` = :L) OR `loc_id` = :L;');
			$stmt->bindValue(':X',$cX);
			$stmt->bindValue(':Y',$cY);
			$stmt->bindValue(':Z',$cZ);
			$stmt->bindValue(':L',$cL);
			$stmt->execute();
			$d = $stmt->fetchAll(PDO::FETCH_ASSOC);
			foreach ($d as $v)
			{
				$this->cellDoors[] = array(
										'id' => $v['id'],
										'X' => $v['X'],
										'Y' => $v['Y'],
										'Z' => $v['Z'],
										'L' => ($v['L'] == $cL?$v['loc_id']:$v['L']),
										'title' => ($v['L'] == $cL?$v['title_i']:$v['title_o']),
									);
			}
		}
		return $this->cellDoors;
	}
	
	public function cells()
	{
		$dC = 5;
		for ($x = -$dC; $x <= $dC; $x++)
		{
			for ($y = -$dC; $y <= $dC; $y++)
			{
				for ($z = -$dC; $z <= $dC; $z++)
				{
					$r[$dC+$x][$dC-$y][$dC+$z] = $this->mapData[$x][$y][$z]['active'];
				}
			}
		}
		return $r;
	}
	
	public function load($cX, $cY, $cZ, $cL)
	{
		$dC = 5;
		$stmt = Engine::get()->db->prepare('SELECT `X`, `Y`, `Z`, `fight`, `active` FROM `map` WHERE `X` BETWEEN :XS AND :XE AND `Y` BETWEEN :YS AND :YE AND `Z` BETWEEN :ZS AND :ZE AND `active` = true;');
		$stmt->bindValue(':XS',$cX - $dC);
		$stmt->bindValue(':XE',$cX + $dC);
		$stmt->bindValue(':YS',$cY - $dC);
		$stmt->bindValue(':YE',$cY + $dC);
		$stmt->bindValue(':ZS',$cZ - $dC);
		$stmt->bindValue(':ZE',$cZ + $dC);
		$stmt->execute();
		for ($x = -$dC; $x <= $dC; $x++)
		{
			for ($y = -$dC; $y <= $dC; $y++)
			{
				for ($z = -$dC; $z <= $dC; $z++)
				{
					$this->mapData[$x][$y][$z] = array('X' => $cX + $x, 'Y' => $cY + $y, 'Z' => $cZ + $z, 'fight' => false, 'active' => false);
				}
			}
		}
		while ($v = $stmt->fetch(PDO::FETCH_ASSOC))
		{
			$this->mapData[$v['X'] - $cX][$v['Y'] - $cY][$v['Z'] - $cZ] = $v;
		}
	}
}

?>
